//
//  DHHomeHotBrandsCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/25.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHHomeHotBrandsCell.h"
#import "DHHomeHotBrands_CollectionCell.h"
#import "DHS2_BrandViewController.h"

@interface DHHomeHotBrandsCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSArray *itemArr; //item数组

@end

@implementation DHHomeHotBrandsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.moreButton.layer.cornerRadius = self.moreButton.height / 2;
    
    WeakSelf(weakSelf)
    [self.moreButton bk_addEventHandler:^(id sender) {
        [weakSelf routerEventWithName:@"change_to_brand" userInfo:nil];
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHHomeHotBrands_CollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHHomeHotBrands_CollectionCell];
}

- (NSArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSArray array];
    }
    return _itemArr;
}

- (void)updateCellWithArray:(NSArray *)arr{
    self.itemArr = arr;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHHomeHotBrands_CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHHomeHotBrands_CollectionCell forIndexPath:indexPath];
    NSDictionary *dic = [self.itemArr objectAtIndex:indexPath.row];
    [cell.imgView dh_setImageURLString:[dic objectForKey:@"logo"]];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat w = (kScreenWidth - 32) / 3;
    CGFloat h = w * 0.51;
    return (CGSize){w,h};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [self.itemArr objectAtIndex:indexPath.row];
    DHS2_BrandViewController *brandController = [[DHS2_BrandViewController alloc] initWithBrandID:[dic objectForKey:@"_id"]];
    brandController.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:brandController animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
